package app

import (
	"fmt"
	"github.com/gin-gonic/gin"
)

/**
 * Created with IntelliJ IDEA.
 * User: 令狐飞侠
 * Date: 2021-11-29
 * Description: 请求参数以及表单
 */
func TestForm() {
	router := gin.Default()

	//get请求带参数
	router.GET("/login", func(context *gin.Context) {
		userName := context.Query("userName")
		fmt.Println("userName:" + userName)
		password := context.Query("password")
		fmt.Println("password:" + password)
		if userName == "" {
			context.JSON(200, gin.H{
				"code": "1",
				"message": "用户名不能为空!",
			})
		} else if password == "" {
			context.JSON(200, gin.H{
				"code": "2",
				"message": "用密码不能为空!",
			})
		}else{
			context.JSON(200, gin.H{
				"code": "3",
				"message": "GET登录成功!",
			})
		}

	})

	//post表单
	router.POST("/login", func(context *gin.Context) {
		userName := context.PostForm("userName")
		fmt.Println("userName:" + userName)
		password := context.PostForm("password")
		fmt.Println("password:" + password)
		if userName == "" {
			context.JSON(200, gin.H{
				"code": "1",
				"message": "用户名不能为空!",
			})
		} else if password == "" {
			context.JSON(200, gin.H{
				"code": "2",
				"message": "用密码不能为空!",
			})
		}else{
			context.JSON(200, gin.H{
				"code": "3",
				"message": "POST登录成功!",
			})
		}
	})

	//启动端口8080的服务
	router.Run(":8080")
}
